package view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import entity.MonthlyReceipt;
import entity.ParkReceipt;
import util.Constants;
import util.TableData;
import util.TableUtil;
import util.Validator;

public class ReportJPanel extends JPanel {
	private static final long serialVersionUID = 2543185054428597662L;

	private JTable parkTable,monthlyTable;			
	private TableData parkTableData,monthlyTableData;
	
	public ReportJPanel(){
        setLayout(new BorderLayout());
        setBorder(BorderFactory.createTitledBorder("收入统计"));
        initTopPanel();
        initParkTablePanel();
        initMonthlyTablePanel();
	}
	
	private void initTopPanel() {
		JPanel topPanel = new JPanel();
		topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
		
		JPanel searchPnl = new JPanel();
		searchPnl.setLayout(new BoxLayout(searchPnl, BoxLayout.X_AXIS));
		
		JPanel inputPnl = new JPanel();
		inputPnl.setLayout(null);

		JLabel lab1 = new JLabel("开始日期",SwingConstants.RIGHT);
		lab1.setFont(Constants.font);
		lab1.setBounds(0, 0, 60, 30);

        JTextField lab1input = new JTextField();
        lab1input.enableInputMethods(false);
        Validator.bind(lab1input,Validator.DATE);
        lab1input.setBounds(65, 0, 120, 30);
        
        JLabel lab2 = new JLabel("截止日期",SwingConstants.RIGHT);
        lab2.setFont(Constants.font);
        lab2.setBounds(200, 0, 60, 30);
        
        JTextField lab2input = new JTextField();
        lab2input.enableInputMethods(false);
        Validator.bind(lab2input,Validator.DATE);
        lab2input.setBounds(265, 0, 120, 30);


        inputPnl.add(lab1);
        inputPnl.add(lab1input);
        inputPnl.add(lab2);
        inputPnl.add(lab2input);
        
        
        JPanel btnBar = new JPanel();
		btnBar.setLayout(new BoxLayout(btnBar, BoxLayout.Y_AXIS));
		
		JButton queryBtn = new JButton("查询");
		queryBtn.setFont(Constants.font);
		queryBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String lab1inputStr = lab1input.getText().trim();
				String lab2inputStr = lab2input.getText().trim();
				String[] params = {lab1inputStr,lab2inputStr};
				refreshTable(params);
			}
		});

		JButton resetBtn = new JButton("重置");
		resetBtn.setFont(Constants.font);
		resetBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				lab1input.setText("");
				lab2input.setText("");
			}
		});
		
		btnBar.add(queryBtn);
		btnBar.add(resetBtn);

		searchPnl.add(inputPnl);
		searchPnl.add(btnBar);
		
		topPanel.add(searchPnl);
		add(topPanel, BorderLayout.NORTH);
	}
	
	private void initParkTablePanel() {
        JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createTitledBorder("停车费收入"));
        panel.setLayout(new BorderLayout());        
		String columns[] = { "日期", "车位类型", "金额"};
		List<String[]> rows = new ArrayList<>();
		parkTableData = new TableData(columns, rows);
		parkTable = new JTable(parkTableData);
		TableUtil.setTableStyle(parkTable);
		JScrollPane jsPane = new JScrollPane(parkTable);
		TableUtil.setJspStyle(jsPane);
		panel.add(jsPane,BorderLayout.CENTER);
		add(panel,BorderLayout.WEST);
	}
	
	private void initMonthlyTablePanel() {
		JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createTitledBorder("月卡收入"));
        panel.setLayout(new BorderLayout());        
		String columns[] = { "日期", "车位类型", "金额"};
		List<String[]> rows = new ArrayList<>();
		monthlyTableData = new TableData(columns, rows);
		monthlyTable = new JTable(monthlyTableData);
		TableUtil.setTableStyle(monthlyTable);
		JScrollPane jsPane = new JScrollPane(monthlyTable);
		TableUtil.setJspStyle(jsPane);
		panel.add(jsPane,BorderLayout.CENTER);
		add(panel,BorderLayout.EAST);
	}
	
	public void refreshTable(String[] params){
		ParkReceipt parkReceipt = new ParkReceipt();		
		List<String[]> prows = parkReceipt.summarize(params);
		parkTableData.refresh(prows);
		
		MonthlyReceipt monthlyReceipt = new MonthlyReceipt();		
		List<String[]> mrows = monthlyReceipt.summarize(params);
		monthlyTableData.refresh(mrows);
	}
}

